package com.trsj.lnsj.util;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.data.domain.Page;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.trsj.lnsj.Constants;
import com.trsj.lnsj.common.DataResponse;
import com.trsj.lnsj.common.MsgStatus;

/**
 * 响应工具类
 *
 * @author HenryYan
 */
public class ResponseHelper {

    public static DataResponse getResponse(int statusCode, String msg) {
        DataResponse res = new DataResponse(statusCode, null);
        res.setMessage(msg);
        return res;
    }

    public static DataResponse getResponse(int statusCode) {
        return new DataResponse(statusCode, null);
    }

    public static DataResponse getSuccessResponse(String key, Object value) {
        JSONObject json = new JSONObject();
        json.put(key, value);
        return getSuccessResponse(json);
    }

    public static DataResponse getSuccessResponse(JSONObject data) {
        return new DataResponse(MsgStatus.STATUS_OK, data);
    }

    public static DataResponse getSuccessResponse(Object data) {
        return new DataResponse(MsgStatus.STATUS_OK, data);
    }

    public static DataResponse getSuccessResponse(JSONArray array) {
        return new DataResponse(MsgStatus.STATUS_OK, array);
    }

    public static DataResponse getSuccessResponse(Map<?, ?> map) {
        return new DataResponse(MsgStatus.STATUS_OK, map);
    }

    public static DataResponse getSuccessResponse(List<?> list) {
        return new DataResponse(MsgStatus.STATUS_OK, list);
    }

    public static DataResponse getSuccessResponse(Page<?> page) {
        return new DataResponse(MsgStatus.STATUS_OK, page);
    }

    public static DataResponse getSuccessResponse(PageInfo<?> page) {
        return new DataResponse(MsgStatus.STATUS_OK, page);
    }

    public static boolean isRequestWithToken(HttpServletRequest request) {
        return request.getParameter(Constants.KEY_TOKEN) != null;
    }

    public static String getWebRoot(HttpServletRequest request) {
        String path = request.getContextPath();
        String scheme = request.getScheme();
        String serverName = request.getServerName();
        int port = request.getServerPort();
        String portStr = (port == 80 ? "" : (":" + port));
        String basePath = scheme + "://" + serverName + portStr + path;
        return basePath;
    }
}
